/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.spmif;

import org.mitre.xmltransform.XmlInputDocument;
import org.mitre.xmltransform.XmlOutputDocument;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ReferenceStep extends HierarchyStep {
	public ReferenceStep(String pId, String pExecId, Node pNode, String pNodeName, XmlInputDocument pInput, XmlOutputDocument pOutput) {
		super(pId, pExecId, pNode, pNodeName, pInput, pOutput);
		mEGName = null;
		mEGId = null;
	}
	
	public String getIdPrefix() {
		return "REF";
	}

	public void setEG(String pEGId, String pEGName) {
		mEGId = pEGId;
		mEGName = pEGName;
	}
	
	public void addChild(HierarchyStep pChild) {
		throw new IllegalArgumentException("Reference steps can not have children");
	}

	public void generateStep(Element pParentElement, Device pLocation) {
		//Node node = getNode();
		//mNodeName = mInput.buildNodeName(node, "REF");
		Element expandedNode = mOutput.addElement(pParentElement, "ExpandedNode", null);
		expandedNode.setAttribute("NodeName", getNodeName());
		expandedNode.setAttribute("NodeId", getId()); //mOutput.getNextId());
/*		if (mInput.evaluateBooleanInputExpression(node, "boolean(self::UML2:InteractionOccurrence)")) {
			mInput.setVariable("interactionId", mInput.getInputValue(node, "UML2:InteractionOccurrence.refersTo/UML2:Interaction/@xmi.idref"));
			expandedNode.setAttribute("EGname", mInput.getInputValue(node, "ancestor::UML:Model/descendant::UML2:Interaction[@xmi.id=$interactionId]/@name"));			
		} else {*/
			if (mEGId == null) throw new IllegalArgumentException("must provide the EG id");
			expandedNode.setAttribute("EGId", mEGId);
			if (mEGName != null) {
				expandedNode.setAttribute("EGname", mEGName);
			}
		//}
	}

	public String generateArcs(Element pParentElement, String pFromNode) {
		String id = getId(); 
		if (id == null) return pFromNode;
		if (pFromNode != null) {
			Element arcNode = mOutput.addElement(pParentElement, "Arc", null);
			arcNode.setAttribute("FromNode", pFromNode);
			arcNode.setAttribute("ToNode", id);
		}
		return id;
	}
	
	private String mEGId;
	private String mEGName;
}